package api

import (
	"net/http"
	"openzbox.com/frame/echo"
	"openzbox.com/misc/ps/load"
	"openzbox.com/utils"

	//"syscall"
	"time"
	//"unsafe"

	//"openzbox.com/utils"
	"openzbox.com/misc/ps/cpu"
	"openzbox.com/misc/ps/disk"
	"openzbox.com/misc/ps/host"
	"openzbox.com/misc/ps/mem"
	_net "openzbox.com/misc/ps/net"
)

type LSysInfo struct {
	/*MemAll         uint64
	MemFree        uint64
	MemUsed        uint64
	MemUsedPercent float64

	Days           int64
	Hours          int64
	Minutes        int64
	Seconds        int64

	CpuUsedPercent float64
	OS             string
	Arch           string
	CpuCores       int
	*/
	Meminfo    *mem.VirtualMemoryStat `json:"meminfo"`
	Cpuinfo    []cpu.InfoStat         `json:"cpuinfo"`
	Hostinfo   *host.InfoStat         `json:"hostinfo"`
	Diskinfo   *disk.UsageStat        `json:"diskinfo"`
	Iocounts   []_net.IOCountersStat  `json:"iocounts"`
	Pids       []int32                `json:"pids"`
	Interfaces _net.InterfaceStatList `json:"interfaces"`
	Load       *load.AvgStat          `json:"load"`
}

// Handler
func Sysinfo(c echo.Context) error {
	info := getSysInfo()
	//fmt.Println(info )
	return c.JSON(http.StatusOK, info)
}

func getSysInfo() (info LSysInfo) {

	//unit := uint64(1024 * 1024) // MB

	v, _ := mem.VirtualMemory()
	info.Meminfo = v

	//info.Pids , _ = process.Pids();
	//fmt.Println(_net.Connections("all"));
	info.Interfaces, _ = _net.Interfaces()
	load, _ := load.Avg()
	info.Load = load

	/*info.MemAll = v.Total
	info.MemFree = v.Free
	info.MemUsed = info.MemAll - info.MemFree
	// 注：使用SwapMemory或VirtualMemory，在不同系统中使用率不一样，因此直接计算一次
	info.MemUsedPercent = utils.RoundFloat64 (float64(info.MemUsed) / float64(info.MemAll) * 100.0 ,2);// v.UsedPercent
	info.MemAll /= unit
	info.MemUsed /= unit
	info.MemFree /= unit*/
	/*
		info.OS = runtime.GOOS
		info.Arch = runtime.GOARCH
		info.CpuCores = runtime.GOMAXPROCS(0)

		// 获取200ms内的CPU信息，太短不准确，也可以获几秒内的，但这样会有延时，因为要等待

	*/
	// 获取开机时间
	/*boottime, _ := host.BootTime()
	ntime := time.Now().Unix()
	btime := time.Unix(int64(boottime), 0).Unix()
	deltatime := ntime - btime

	info.Seconds = int64(deltatime)
	info.Minutes = info.Seconds / 60
	info.Seconds -= info.Minutes * 60
	info.Hours = info.Minutes / 60
	info.Minutes -= info.Hours * 60
	info.Days = info.Hours / 24
	info.Hours -= info.Days * 24
	*/
	cc, _ := cpu.Percent(time.Millisecond*200, false)

	c, _ := cpu.Info()
	c[0].CpuUsedPercent = utils.RoundFloat64(cc[0], 2)

	info.Cpuinfo = c
	h, _ := host.Info()
	info.Hostinfo = h

	d, _ := disk.Usage("/")
	info.Diskinfo = d

	nv, _ := _net.IOCounters(true)
	info.Iocounts = nv

	/*d, _ := disk.Usage("/")
	nv, _ := _net.IOCounters(true)
	physicalCnt, _ := cpu.Counts(false)
	logicalCnt, _ := cpu.Counts(true)
	if len(c) > 1 {
		for _, sub_cpu := range c {
			modelname := sub_cpu.ModelName
			cores := sub_cpu.Cores
			fmt.Printf("CPUs: %v   %v cores \n", modelname, cores)
		}
	} else {
		sub_cpu := c[0]
		modelname := sub_cpu.ModelName
		cores := sub_cpu.Cores
		fmt.Printf("CPU: %v   %v cores \n", modelname, cores)
	}
	fmt.Printf("physical count:%d logical count:%d\n", physicalCnt, logicalCnt)
	fmt.Printf("HD: %v MB Free: %v MB Usage:%.2f%%\n", d.Total/1024/1024, d.Free/1024/1024, d.UsedPercent)
	fmt.Printf("OS: %v(%v) %v\n", n.Platform, n.PlatformFamily, n.PlatformVersion)
	fmt.Printf("Hostname: %v\n", n.Hostname)
	fmt.Printf("Network: %v bytes / %v bytes\n", nv[0].BytesRecv, nv[0].BytesSent)
	*/
	//GetMACAddress()

	/*r ,err :=utils.Ping("www.baidu.com")
	fmt.Printf("%v %s\r\n",r,err)
	*/
	return info

}
